import { Component, OnInit } from '@angular/core';

import { ThreeLibsService } from 'src/app/core/service/three-libs.service';
import { ContainerService } from 'src/app/core/service/container.service';
import { LibraryService } from 'src/app/core/service/library.service';

@Component({
  selector: 'app-container-list',
  templateUrl: './container-list.component.html',
  styleUrls: ['./container-list.component.scss']
})
export class ContainerListComponent implements OnInit {
  // 定义变量
  isVisibleTop = false;
  isVisibleMiddle = false;

  applicationsName = '';
  appList = '';

  data = [
    {
      title: '基线名称',
      content: 'JBoss目录列表安全基线'
    },
    {
      title: '基线编号',
      content: 'BSM-JSFJKDS-7899-9090'
    },
    {
      title: '基线项说明',
      content: '禁止MIJI列表显示文件'
    },
    {
      title: '检测步骤',
      content: '第一步...第二步'
    },
    {
      title: '判定依据',
      content: '判定条件、检测操作'
    },
    {
      title: '备注',
      content: '....'
    }
  ];

  allChecked = false;
  indeterminate = false;
  displayData = [];

  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页

  dataSet = [];       // 数据集合

  loading = true;     // 加载

  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  searchGenderList: string[] = [];
  filter: string;

  constructor(
    private threelibsservice: ThreeLibsService,
    private containerService: ContainerService,
    private libraryservice: LibraryService,

  ) { }
  /**
   * @param reset 获取数据
   */
  getContainerPage(reset: boolean = false): void {
    console.log('显示数据===》');
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;
    this.containerService.getContainerPage(this.pageIndex, this.pageSize, this.filter).subscribe(
      (containerPage) => {
        console.log(containerPage, '<--- containerPage');
        this.loading = false;
        for (let i = 0; i < containerPage.data.length; i++) {
          if (containerPage.data[i].appList.length === 0) {
            containerPage.data[i].appList.push(
              {
                appUuid: '',
                name: '暂无应用'
              });
            console.log(typeof containerPage.data[i].appList);
          }
          if (containerPage.data[i].releaseDate === null || containerPage.data[i].releaseDate === '') {
            containerPage.data[i].releaseDate = '暂无记录';
            console.log('Janine', typeof containerPage.data[i].releaseDate);
            console.log('Janine', containerPage.data[i].releaseDate.replace(null, '暂无记录'));
          }
        }
        this.dataSet = containerPage.data;
        this.total = containerPage.total;
      }
    );
  }

  updateFilter(value: string[]): void {
    this.searchGenderList = value;
    // this.searchData(true);
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }


  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  /**
   * 全选方法
   */
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    // 刷新状态
    this.refreshStatus();
  }

  // 对话框
  showModalMiddle(): void {
    this.isVisibleMiddle = true;
  }

  handleOkMiddle(): void {
    console.log('click ok');
    this.isVisibleMiddle = false;
  }

  handleCancelMiddle(): void {
    this.isVisibleMiddle = false;
  }

  getDownloadFile() {
    console.log('获取下载的文件');
    const injectProxyId = 'default';
    this.libraryservice.downloadFile(injectProxyId)
      .subscribe(data => {
        const agentFile = new Blob([data], { type: 'application/octet-stream' });
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
          window.navigator.msSaveOrOpenBlob(agentFile, 'sca.jar');
        } else {
          const link = document.createElement('a');
          document.body.appendChild(link);
          link.style.visibility = 'hidden';
          link.href = window.URL.createObjectURL(agentFile);
          link.download = 'sca.jar';
          link.click();
          document.body.removeChild(link);
        }
      });
  }

  ngOnInit(): void {
    this.getContainerPage();
  }

}
